home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4763 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  6.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.sys.sgi.misc,comp.lang.c,comp.unix.programmer
  4. Subject: Re: memory management in UNIX (SGI IRIX)
  5. Date: 6 Feb 1996 13:50:43 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4f8ifjINN7qh@keats.ugrad.cs.ubc.ca>
  8. References: <DM0Jv9.9F4@bii.bruker.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DM0Jv9.9F4@bii.bruker.com>, Joe Meier <jem@bii.bruker.com> wrote:
  12. >   We have a problem with a program running under UNIX (SGI IRIX 5.x).  
  13. >It uses X11 for a graphical user interface.  
  14. >The program allocates and frees large amounts of memory for data 
  15. >(sometimes chunks as big as 20M).  The problem is that in a relatively
  16. >short time the system runs out of swap sapce and we have to kill the program.
  17. >We have investigated the problem and it seems to be that the allocated memory
  18. >is getting segmented, so that even though the memory is freed by calls to
  19. >"free", the next malloc does not necessairly use the freed space because it
  20. >cannot find a large enough contiguous block and instead allocates more from
  21. >the system.  When we were working with smaller data sizes this was not a 
  22. >problem, but the bigger data sizes is causing some svere problems.  The 
  23. >first question is whether anyone else has encountered similar problems
  24. >and what caused them and how they solved it?  Also, I wanted to know if 
  25. >there exists any intelligant memory managers that will move memory blocks
  26. >in order to keep all the free space in on contiguous location?  I think this
  27. >would go a long way towards solving our problems???  Of-course if anyone 
  28. >has other sugestions for what the cause of our problems are, they would be
  29. >greatly appreciated.
  30.  
  31. These problems have been researched since the dope-smoking 60's and probably
  32. earlier.
  33.  
  34. There are two ways to do dynamic memory allocation. (This is what we are talking
  35. about. "Memory Mangement" refers to the strategies implemented in the operating
  36. system for assigning memory to tasks and OS procedures. )
  37.  
  38. You can allocate in such a way that the address of a block does not change, or
  39. you can use an extra level of indirection that allows you to perform "heap
  40. compaction" to coalesce unused free areas into one big free block. THis is not
  41. the same as "garbage collection". The latter refers to an allocation scheme in
  42. which free blocks are _never_ reused, nor kept track of. When the heap runs out
  43. of space, the garbage compactor loops over the allocated blocks and squeezes
  44. them together to take up the slack in between. There is never a list of free
  45. blocks, that's why these areas are called "garbage".
  46.  
  47. If you don't allow for heap compaction, there is no general method to avoid
  48. fragmentation. In a JACM article appearing in, I think, vol. 18 of the JACM
  49. (circa 1971), a fellow by the name of J. M. Robson published a proof that
  50. a program which allocates and frees blocks that are up to 2^b cells in size
  51. such that the total memory allocated never exceeds N cells will, in the worst
  52. case, grow to occupy a footprint of  N * (1+b) cells, such that any further
  53. requests for a block that is 1..2^b large will fail and require that the heap
  54. increase further.
  55.  
  56. Robson proved this result only for block sizes that are dyadic powers: 1, 2, 4,
  57. 8, 16, ... 2^b.
  58.  
  59. Still, it is serious. If you are allocating blocks up to 4KB (2^12 bytes) and
  60. over the lifetime of your program, a maximum of only 1MB is claimed at any one
  61. time, you could end up at a situation where the program has a 13MB heap and yet
  62. cannot satisfy the largest possible request (4KB), which also means that it's
  63. unlikely that "holes" in the heap can be returned to the OS in the form of
  64. discrete pages, if your OS uses 4KB pages or larger.
  65.  
  66. Since you are allocating about 20MB blocks, you are looking at, say, a max
  67. block size of 2^25 (the next highest power of two for the sake of convenience).
  68. Expect your worst case allocation behaviour to consume 16x as much memory as it
  69. is supposed to. For example, if your program uses 50MB, you could eat 800MB
  70. (assuming you have it). This is assuming that you can have very small
  71. allocations all the way down to one byte. Say that you never allocate less than
  72. a megabyte. You can then work in megabyte units. You allocate blocks of up to
  73. 2^5 (32) megabytes, and could claim up to six times as much memory as you ask
  74. for, or 300MB out with only 50MB allocated. 
  75.  
  76. Of course, the worst case scenario is accomplished by an optimal "attacker"
  77. (requestor) going against an optimal "defender" (allocator strategy). The
  78. actual results in a real application may be better, and can be improved by
  79. using a different allocator strategy.
  80.  
  81. I suggest you look for alternative libraries, and don't count out heap
  82. compaction. It's possible to design allocators that allow for heap compaction,
  83. yet have entry points that are backward-compatible with malloc() and friends
  84. (these will "lock" the allocated chunk in place so that it cannot be moved
  85. during compaction, satisfying the malloc() paradigm that the address of a block
  86. doesn't change).
  87.  
  88. There are all kinds of strategies for malloc-type allocation: best fit, quick
  89. fit, binary buddies, fibonacci buddies, etc.
  90.  
  91. Check out the Knuth volumes as well as that JACM article if you want to read
  92. Robson's gloomy number-theoretical proof.
  93.  
  94. Look at the design of your program and see if you can't improve the behavior by
  95. restructuring the way it does allocation. The order of allocations matters. A
  96. strategic "attacker" wishing to maximize memory waste (with constraints on
  97. total memory and maximum block size) depends on a devilish order of allocations
  98. which forces the defending player to helplessly dole out memory.
  99.  
  100.  
  101.  
  102. >   Since, I am cross posting this to several news groups, I ask that
  103. >responses be e-mailed directly to me (at jem@bruker.com) so that I can more
  104. >eaisly keep track of them.  I will post a summary (for those interested) once
  105. >I have complied the responses.
  106.  
  107. Also, consider enlisting the aid of the UNIX operating system. Since you are
  108. working with large blocks, your heap will contain some large holes. These holes
  109. _can_ be returned to the operating system so that they are only virtual holes
  110. and don't use up any (or hardly any) memory. 
  111.  
  112.  
  113. Have you considered using anonymous memory maps via the mmap()/munmap() system
  114. calls? When you munmap() a mmap()ed region, it is truly _gone_. Your OS has a
  115. large virtual address space in which to place memory mapped objects. Even if
  116. you waste a half a page on each one (due to the alignment of mapped regions),
  117. you will still win by eliminating the heap problem.
  118.  
  119. Try mmap!
  120. -- 
  121.  
  122.